#!/bin/bash -xe

kata_coco_top_dir="$HOME/workspace/CoCo/"

kata_containers_repo_url="https://gitee.com/hanliyang-kata-coco/kata-containers.git"
kata_containers_repo_branch="3.13.0-hygon-arch-sig"
kata_containers_repo_dir="${kata_coco_top_dir}/kata-containers"

if [ ! -e ${kata_containers_repo_dir} ]; then
  git clone ${kata_containers_repo_url} \
    -b ${kata_containers_repo_branch} \
    ${kata_containers_repo_dir}
fi

export KERNEL_MODULES_DIR="/dev/shm/openanolis-kernel/lib/modules/6.6.71+/"
export AGENT_TARBALL="/dev/shm/kata-agent-init/kata-agent-init.tar.xz"
export COCO_GUEST_COMPONENTS_TARBALL="/dev/shm/guest-components.tar.xz"
export PAUSE_IMAGE_TARBALL="${kata_containers_repo_dir}/tools/packaging/kata-deploy/local-build/build/kata-static-pause-image.tar.xz"

export distro="centos" # example
export OS_VERSION="stream10" # version of centos
export rootfs_builder_dir="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/rootfs-builder)"
export initrd_builder_dir="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/initrd-builder)"
export ROOTFS_DIR="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/rootfs-builder/rootfs)"
export scripts_lib_sh="$(realpath ${kata_containers_repo_dir}/tools/osbuilder/scripts/lib.sh)"

if [ "X${distro}" = "Xcentos" -a "X${OS_VERSION}" = "Xstream10" ]; then
  # enable gpgcheck will fail on centos:stream10
  pushd ${kata_containers_repo_dir}
  git checkout ${scripts_lib_sh}
  sed -i "s/gpgcheck=1/gpgcheck=0/g" ${scripts_lib_sh}
  popd # ${kata_containers_repo_dir}
fi

# check
pr_err () {
  echo -e "\033[31;7m[Error] $*\033[0m"
}

check_prerequisites () {
  echo "[Check Prerequisites]"
  local elems=(${KERNEL_MODULES_DIR} \
               ${AGENT_TARBALL} \
               ${COCO_GUEST_COMPONENTS_TARBALL} \
               ${PAUSE_IMAGE_TARBALL} \
              )
  for elem in ${elems[@]}; do
    if [ ! -e ${elem} ]; then
       pr_err "missing ${elem}"
       exit 1
    fi
  done
}

check_prerequisites

# build initrd (INIT=yes, AGENT_INIT=yes)
export GOPATH="$HOME/go"

build_rootfs_for_initrd () {
  echo "[Build rootfs for kata guest initrd]"
  pushd ${rootfs_builder_dir}

  sudo rm -rf "${ROOTFS_DIR}"

  # MEASURED_ROOTFS=yes
  script -fec 'sudo -E DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} MEASURED_ROOTFS=yes AGENT_INIT=yes AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./rootfs.sh "${distro}"'

  # MEASURED_ROOTFS=no
  #script -fec 'sudo -E DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} AGENT_INIT=yes AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./rootfs.sh "${distro}"'

  popd # ${rootfs_builder_dir}
}

build_rootfs_for_initrd

build_install_initrd () {
  echo "[Build and Install kata guest initrd]"
  pushd ${initrd_builder_dir}

  # MEASURED_ROOTFS=yes
  script -fec 'sudo -E DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} MEASURED_ROOTFS=yes AGENT_INIT=yes AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./initrd_builder.sh "${ROOTFS_DIR}"'

  # MEASURED_ROOTFS=no
  #script -fec 'sudo -E DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain USE_DOCKER=true AGENT_TARBALL=${AGENT_TARBALL} CONFIDENTIAL_GUEST=yes COCO_GUEST_COMPONENTS_TARBALL=${COCO_GUEST_COMPONENTS_TARBALL} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR} AGENT_INIT=yes AGENT_POLICY=yes PAUSE_IMAGE_TARBALL=${PAUSE_IMAGE_TARBALL} ./initrd_builder.sh "${ROOTFS_DIR}"'

  install_top_dir=/opt/kata
  install_initrd_dir=$install_top_dir/share/kata-containers/
  mkdir -p $install_initrd_dir || true
  commit="$(git log --format=%h -1 HEAD)"
  date="$(date +%Y-%m-%d-%T.%N%z)"
  initrd="kata-containers-initrd-${date}-${commit}-csv.initrd"
  sudo install -o root -g root -m 0644 -D kata-containers-initrd.img "$install_initrd_dir/${initrd}"
  cd $install_initrd_dir && sudo ln -sf "$initrd" kata-containers-initrd-confidential-csv.img

  popd # ${initrd_builder_dir}
}

build_install_initrd
